/* interface/hiir/hiir.c
 *
 * Copyright (c) 2006 Hisilicon Co., Ltd. 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * History:
 *      16-12-2006 Start of Hi3560 CPU support
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <asm/hardware.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/device.h>
#include <linux/clk.h>

#include <asm/io.h>
#include <asm/sizes.h>
#include <asm/arch/hardware.h>

#include "vi-base.h"



/* define IR IO */
#define VI_REG_BASE (IO_ADDRESS(0x10100000))

#define VI_BASE(reg)	(IO_ADDRESS(0x10100000 + reg))

#define VI_BASE_test	(IO_ADDRESS(0x10100000 + 04))


/* define macro */
#define WRITE_REG(Addr, Value) ((*(volatile unsigned int *)(Addr)) = (Value))
#define READ_REG(Addr)         (*(volatile unsigned int *)(Addr))

void __iomem *base;

unsigned long read_long(unsigned long reg)
{
	unsigned long value;
	//void __iomem *base;
	//printk("read long = 0x%lx\n", reg);
	//return readl(VI_BASE(reg));
	//value = readl(VI_BASE(reg));
	/*base = ioremap(VI_REG_BASE, PAGE_SIZE);
	if (!base) {
		printk("ioremap error\n");
		return 0x00000;
	}*/
	value = readl(base + reg);
	printk("read value = 0x%lx\n", value);

	//READ_REG(VI_BASE(reg));
	//printk("read value = 0x%lx\n", value);

	return value;
}

unsigned int write_vi_base(vi_base_reg * vi)
{
	//unsigned long temp;
	//void __iomem *base;

	printk("[reg %lx = 0x%lx \n", vi->reg, vi->value);
	//writel(vi->value, VI_BASE(vi->reg));

	/*base = ioremap(VI_REG_BASE, PAGE_SIZE);
	if (!base) {
		printk("ioremap error\n");
		return 0x00000;
	}*/
	writel(vi->value, base + vi->reg);
	

	//temp = readl(VI_BASE(vi->reg));
	//vi->value = readl(VI_BASE((long)vi->reg);
	//printk("[value = 0x%lx\n", temp);

	return 0;
}

static int vi_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
    int __user *argp = (int __user *)arg;
	vi_base_reg vi;
	unsigned long reg;
	unsigned long value;

    switch(cmd)
    {
        case VI_BASE_GET_ALL_REG:
			//vi_all_reg();
			break;

		case VI_BASE_GET_SIGNLE_REG:
			if (copy_from_user(&reg, argp, sizeof(reg))) {
				printk(KERN_ALERT "\t[ioctl error!] VI_BASE_GET_SIGNLE_REG\n");
				return -EINVAL;
			}
			//printk("VI_BASE_GET_SIGNLE_REG reg = %lx\n", reg);
			value = read_long(reg);
			//printk("VI_BASE_GET_SIGNLE_REG value = 0x%x\n", value);
			//printk("aaaa reg = 0x%x\n", reg);
			if (copy_to_user(argp, &value, sizeof(long)))
				return -1;

			break;

		case VI_BASE_SET_SIGNLE_REG:
			if (copy_from_user(&vi, argp, sizeof(vi))) {
				printk(KERN_ALERT "\t[ioctl error!] VI_BASE_SET_SIGNLE_REG\n");
				return -EINVAL;
			}
			write_vi_base(&vi);
			break;

        default:
            printk( "Error: Inappropriate ioctl for cmd\n");
            return -ENOTTY;
    }

    return 0;
}

static int vi_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static int vi_close(struct inode *inode, struct file *filp)
{
    return 0;
}


static struct file_operations vi_fops =
{
    owner   : THIS_MODULE,
    open    : vi_open,
    ioctl   : vi_ioctl,
    release    : vi_close,
};

static struct miscdevice vi_miscdev =
{
    .minor = MISC_DYNAMIC_MINOR,
    .name  = VI_DEVICE_NAME,
    .fops  = &vi_fops,
};

static int __init vi_init(void)
{
    int res = 0;

    //printk("\tvi modify!\n");

	res = misc_register(&vi_miscdev);
	if (0 != res)
	{
		printk(KERN_DEBUG "Error: can't register\n");
		return -EFAULT;
	}

	//base = ioremap(VI_REG_BASE, PAGE_SIZE);
	base = (void*)VI_REG_BASE;
	if (!base) {
		printk("ioremap error\n");
		return 0x00000;
	}
        
    printk("\tvi base!.\n");

    return res;
}

static void __exit vi_exit(void)
{
    misc_deregister(&vi_miscdev);
}

module_init(vi_init);
module_exit(vi_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("VI Base Device Driver");
MODULE_VERSION("HI_VERSION=" OSDRV_MODULE_VERSION_STRING);



